package com.team7.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.team7.common.entity.ZoneCourse;
import com.team7.common.vo.AddCourseVo;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.CourseVo;
import com.team7.home.dao.ZoneCourseDao;
import com.team7.home.feign.CoreFeign;
import com.team7.home.service.ZoneCourseService;
import com.team7.home.util.ShujuUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: team7-object-master
 * @description:
 * @author: 王金涛
 * @create: 2021-10-19 14:51
 **/
@Service
public class ZoneCourseServiceImpl extends ServiceImpl<ZoneCourseDao, ZoneCourse> implements ZoneCourseService {
    @Resource
    private ZoneCourseDao zoneCoursedao;

    @Resource

    private CoreFeign coreFeign;


    @Override
    public CommonResult getAllDetail(Integer page, Integer limit, CourseVo courseVo) {
        Page<ShujuUtil> page1 = new Page<>(page, limit);
        Page<ShujuUtil> allDetail = zoneCoursedao.getAllDetail(page1, courseVo.getZoneId(),courseVo.getCourseTitle(),courseVo.getIsDisable());
        if (allDetail!=null){
            return new CommonResult(2000,"查询成功",allDetail);
        }else{
            return new CommonResult(2000,"查询失败",null);
        }
    }

    @Override
    public CommonResult changeStatus(String id, Integer isDisable) {
//        ZoneCourse zoneCourse = new ZoneCourse();
        ZoneCourse zoneCourse = new ZoneCourse();
        zoneCourse.setId(id);
        zoneCourse.setIsDisable(isDisable);
        int i = zoneCoursedao.updateById(zoneCourse);
        if (i>0){
            return new CommonResult(2000,"更改成功",null);
        }else {
            return new CommonResult(5000,"更改失败",null);
        }
    }

    @Override
    public CommonResult deleteZone(String id) {
        ZoneCourse course = new ZoneCourse();
        course.setId(id);
        course.setIsDeleted(1);
        int i = zoneCoursedao.deleteById(course);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }

    @Override
    public CommonResult getAllCourse() {
        List<ZoneCourse> zoneCourses = zoneCoursedao.selectList(null);
        return new CommonResult(2000,"成功",zoneCourses);
    }

    @Override
    @Transactional
    public CommonResult addCourseDetail(AddCourseVo addCourse) {
        ZoneCourse zoneCourse = new ZoneCourse();
        zoneCourse.setCourseId(addCourse.getCourseId());
        zoneCourse.setZoneId(addCourse.getZoneId());
        zoneCourse.setGmtCreate(LocalDateTime.now());
        zoneCourse.setGmtModified(LocalDateTime.now());
        zoneCourse.setIsDeleted(0);
        zoneCourse.setIsDisable(0);
        int i = zoneCoursedao.insert(zoneCourse);
        Integer integer = coreFeign.updateAdd(addCourse.getCourseId());
        if (i>0 && integer>0){
            return new CommonResult(2000,"修改成功",null);
        }else {
            return new CommonResult(5000,"修改失败", null);
        }
    }

    @Override
    public CommonResult get(Integer page, Integer limit, CourseVo courseVoz) {
        QueryWrapper<ZoneCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("zone_id",courseVoz.getZoneId());
        CommonResult courseByPage = coreFeign.findCourseByPage(page, limit, courseVoz, null);
        Object result = courseByPage.getResult();
        ObjectMapper mapper = new ObjectMapper();
        Page page1 = mapper.convertValue(result, Page.class);
        List records = page1.getRecords();
        ArrayList<CourseVo> courseVos = new ArrayList<>();
        for (Object record:records) {
            CourseVo courseVo = mapper.convertValue(record, CourseVo.class);
            QueryWrapper<ZoneCourse> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("zone_id",courseVoz.getZoneId());
            wrapper1.eq("course_id",courseVo.getId());
            wrapper1.eq("is_deleted",0);
            List<ZoneCourse> zoneCourses = zoneCoursedao.selectList(wrapper1);
            if (zoneCourses.size()>0){
                courseVo.setAdd(1);
            }else {
                courseVo.setAdd(0);
            }
            courseVos.add(courseVo);
            System.out.println(courseVos);
        }
        page1.setRecords(courseVos);
        return new CommonResult(2000,"查询成功",page1);
    }
}
